package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.server.ServerCloneException;
import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author lynn
 * @since 2025-03-12
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Override
    public List<CategoryBrandVo> selectCategoryBrandList(Integer brandId, Integer categoryId) {
        List<CategoryBrandVo> list = categoryBrandMapper.selectCategoryBrandList(brandId,categoryId);
        return list;
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        //判断是否已经存在
        Long count = categoryBrandMapper.selectCount(new LambdaUpdateWrapper<CategoryBrand>()
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
        if(count>0){
            throw new ServiceException("该分类下品牌已存在");
        }else {
            return categoryBrandMapper.insert(categoryBrand);
        }

    }

    @Override
    public List<Brand> getBrandByCategoryId(Integer categoryId) {
        List<Brand> list = categoryBrandMapper.getBrandByCategoryId(categoryId);
        return list;
    }
}
